package br.com.cardoso.videostore.web.jsf;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.cardoso.application.ApplicationConstants;
import org.cardoso.web.jsf.FacesUtils;

import br.com.cardoso.videostore.model.entity.UserEntity;

@ManagedBean(name = "applicationMB")
@SessionScoped
public class ApplicationMB {
	
	public ApplicationMB() {
		
	}
	
	private void destroy() {
		
	}
	
	public void logout() {
		try {
			FacesUtils.setSessionAttribute(ApplicationConstants.USER_LOGGED, null);
			
			destroy();
			
			FacesUtils.sendRedirect("login.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getNameUserLogged() {
		
		if (getUserLogged() != null)		
			return getUserLogged().getName();
		else
			return "";
	}
	
	private UserEntity getUserLogged() {
		return (UserEntity) FacesUtils.getSessionAttribute(ApplicationConstants.USER_LOGGED);
	}

}